#include "browser_viwe.h"

/**
 * 加载url
 */
void browser_load_url(cef_browser_view_t *browser_view, char *url) {
    cef_browser_t *cef_browser = browser_view->get_browser(browser_view);
    cef_frame_t *main_frame = cef_browser->get_main_frame(cef_browser);
    cef_string_utf16_t url_str = {};
    cef_string_utf8_to_utf16(url, strlen(url), &url_str);
    main_frame->load_url(main_frame, &url_str);

    cef_string_utf16_clear(&url_str);
    LOG_DEBUG("end");
}

/**
 * 加载本地html,
 * 路径相对于exe文件所在目录
 * 使用前需要调用register_scheme_http注册协议
 */
void browser_load_file(cef_browser_view_t *browser_view, const char *html) {
    const char *prefix = "http://local_file/";
    size_t length = strlen(prefix) + strlen(html) + 1;
    char *url = (char *)malloc(length);
    if (url == NULL) {
        // 内存分配失败
        fprintf(stderr, "load_file Memory allocation failed\n");
        return;
    }
    sprintf(url, "%s%s", prefix, html);
    cef_browser_t *cef_browser = browser_view->get_browser(browser_view);
    cef_frame_t *main_frame = cef_browser->get_main_frame(cef_browser);

    cef_string_t url_str = {};
    cef_string_utf8_to_utf16(url, strlen(url), &url_str);
    main_frame->load_url(main_frame, &url_str);

    cef_string_utf16_clear(&url_str);
    free(url);
    LOG_DEBUG("log:  cef_register_scheme_handler_factory finish-------------\n");
}